class Hero {
  hp = 0
  minAp = 0 // 最小攻击力
  maxAp = 0 // 最大攻击力
  dodgeRate = 0 // 闪避
  strikeRate = 0 // 暴击
  name = ''

  static heroCount = 0
  static createHero(
    name, hp, minAp, maxAp, strikeRate, dodgeRate
  ) {
    const hero = new this()
    hero.id = 'h' + (++this.heroCount)
    hero.name = name
    hero.hp = hp
    hero.minAp = minAp
    hero.maxAp = maxAp
    hero.strikeRate = strikeRate
    hero.dodgeRate = dodgeRate
    return hero
  }
  static attackLog(h1, h2, ap, strike, dodge) {
    return `
      <div class='log ${
        strike ? 'strike':''
      } ${dodge ? 'dodge':''}'>
        <span class='${h1.id}'>${h1.name}</span> ${
          strike ?
            `奋力一击产生 ${ap*2} 伤害` :
            `发起攻击产生 ${ap} 伤害`
        }，${
          dodge ? `被<span class='${h2.id}'>
            ${h2.name}</span> 闪开了！` : ``
        } <span class='${h2.id}'>${h2.name}</span>
          还有${h2.hp}血量
      </div>
    `
  }
  static deathLog(h1, h2) {
    return `<div class='log death'>
      <span class='${h2.id}'>${h2.name}</span>输了,
      <span class='${h1.id}'>${h1.name}</span>
      还有${h1.hp}血
    </div>`
  }

  get death() { return this.hp<=0}
  attack(hero) {
    const ap =
      Math.floor(
        Math.random() * (this.maxAp - this.minAp)
      ) + this.minAp // 攻击力
    const strike = Math.random() < this.strikeRate
    const dodge = Math.random() < hero.dodgeRate
    hero.hp = hero.hp -
      (dodge ? 0 : (strike ? ap*2 : ap))
    return Hero.attackLog(
      this, hero, ap, strike, dodge
    )
  }
}

class SHero extends Hero {
  static skillLog(h1, h2) {
    return `
      <div class='log skill'>
        <span class="${h1.id}">${h1.name}</span>
        打出普通的一拳，
        <span class="${h2.id}">${h2.name}</span>
        死了
      </div>
    `
  }
  static idleLog(h1, h2) {
    return `
      <div class='log idle'>
        <span class="${h1.id}">${h1.name}</span>
        看了看天上的浮云...
      </div>
    `
  }

  attack(h2) {
    const skill = Math.random() < 0.5
    if (skill) {
      h2.hp = 0
      return SHero.skillLog(this, h2)
    } else {
      return SHero.idleLog(this, h2)
    }
  }
}